package com.tz.system.role.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tz.system.role.dao.RolePrivateDao;
import com.tz.system.role.model.TMgtRolePrivate;
import com.tz.system.role.service.RolePrivateService;

/**
 * 
 * @author mike
 *
 */

@Service
@Transactional
public class RolePrivateServiceImpl implements RolePrivateService {
	
	@Autowired
	private RolePrivateDao rolePrivateDao;
	
	//@Autowired
	//private RolePrivateVoDao rolePrivateVODao;
	
	
	private static String rolePrivateHql = "from TMgtRolePrivate where 1=1 and roleCode = ? ";
	
	@Override
	@Transactional
	public List<TMgtRolePrivate> getListByRoleCode(String roleCode) {
		return rolePrivateDao.find(rolePrivateHql, roleCode);
	}
	
	@Transactional
	public void saveRolePrivateList(List<TMgtRolePrivate> list){
		if(list!=null && list.size()>0 ){
			for(int i =0; i<list.size(); i++){
				rolePrivateDao.save(list.get(i));
			}
		}
	 }

	@Override
	@Transactional
	public void saveRolePrivate(TMgtRolePrivate entity) {
		this.rolePrivateDao.save(entity);
	}

	@Override
	@Transactional
	public void removeRolePrivate(TMgtRolePrivate entity) {
		this.rolePrivateDao.removeById(entity.getId());
	}

	@Override
	@Transactional
	public void removePrivateByRoleCode(String roleCode){
		List<TMgtRolePrivate> list = getListByRoleCode(roleCode);
		removeRolePrivateList(list);
	}
	
	@Override
	@Transactional
	public void removeRolePrivateList(List<TMgtRolePrivate> list) {
		if(list !=null && list.size()>0){
			for(TMgtRolePrivate entity:list){
				this.rolePrivateDao.removeById(entity.getId());
			}
		}
	}

	@Override
	@Transactional
	public void removeRpeById(Integer id) {
		this.rolePrivateDao.removeById(id);
	}

	@Override
	@Transactional
	public void removeRpByIdList(List<Integer> list) {
		if(list !=null && list.size()>0){
			for(Integer id:list){
				this.rolePrivateDao.removeById((id));
			}
		}
	}
	@Override
	@Transactional
	public TMgtRolePrivate checkExists(String roleCode,String tMcode) {
		List<TMgtRolePrivate> result = this.rolePrivateDao.find("from TMgtRolePrivate where roleCode=? and menuCode=?",roleCode,tMcode);
		if(result!=null && result.size()>0)
			return result.get(0);
		else 
			return null;
	}

	@Override
	@Transactional
	public List<TMgtRolePrivate> queryMenuCode(String roleCode) {
		return rolePrivateDao.find("from TMgtRolePrivate where roleCode=?", roleCode);
	}
	
	@Override
	@Transactional
	public boolean checkSonExists(String roleCode, String tMcode){
		List<TMgtRolePrivate> result = this.rolePrivateDao.find("from TMgtRolePrivate where roleCode=? and menuCode<>? and menuCode like ?",roleCode,tMcode,tMcode+"%");
		if(result!=null && result.size()>0){
			return true;
		}else{ 
			return false;
		}
	}

	@Override
	public void removeRolePrivate(String roleCode, String tMcode) {
		String hql = "delete from TMgtRolePrivate where roleCode=? and menuCode=?";
		rolePrivateDao.executeByHql(hql,roleCode,tMcode);
	}
	
	
}
